-- @owner: opentestcase023
-- @date: 2022/02/24
-- @testpoint: 在更新src视图前，将src表中id1最小值对应id3值更新到des表中，并将视图更新对应值过更新至src表

--step1:建表;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;
create table t_trigger_src(id1 int, id2 int, id3 int) with (storage_type=ustore);
create table t_trigger_des(id1 int, id2 int, id3 int) with (storage_type=ustore);

--step2:在更新src视图前，将src表中id1最小值对应id3值更新到des表中，并将视图更新对应值过更新至src表;expect:成功
create or replace view t_trigger_src_view1 as select * from t_trigger_src;
create or replace function tri_update_func() returns trigger as
$$
  declare
  begin
  update t_trigger_des set id3 = (select id3 from t_trigger_src order by id1 limit 1);
  return old;
  end
$$ language plpgsql;
/
create trigger update_trigger before update on t_trigger_src_view1
   execute procedure tri_update_func();
/
insert into t_trigger_src values(1,1,1),(2,2,2);
insert into t_trigger_des values(1,1,1),(2,2,2);
select * from t_trigger_des order by id1;
create or replace function update_func()
returns trigger as $$
begin
update t_trigger_src set id3=new.id3 where id1=old.id1;
return new;
end;
$$language plpgsql;
/
drop trigger if exists insert_trigger_1 on t_trigger_src_view1;
create trigger insert_trigger_1
instead of update on t_trigger_src_view1
for each row
execute procedure update_func();
/
update t_trigger_src_view1 set id3=10 where id1=1;
select * from t_trigger_des order by id1;
select * from t_trigger_src order by id1;

--step3:清理环境;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;